<?php


namespace App\Http\Traits;


use Illuminate\Support\Arr;

class GlobalConstant
{

    //统一数组分隔符
    const DELIMITER_CHARACTER = ",";
    const IS_SUG              = [
        1 => '是',
        2 => '否'
    ];


    const ROLE_LIST = [
        1 => '群主',
        2 => '管理员',
        3 => ''
    ];

    const _STATUS = [
        0 => '禁用',
        1 => '正常'
    ];


    public static function getRoleMap($key = "ALL")
    {
        $ret = self::ROLE_LIST;
        if ($key === "ALL") {
            return $ret;
        }
        return Arr::get($ret, $key, false);
    }

    public static function sortGoodsPrice($goodsList)
    {
        if (is_array($goodsList)) {
            usort($goodsList, function ($a, $b) {
                return (float)$a['price'] <=> (float)$b['price'];
            });
            return $goodsList;
        }
        $temp = $goodsList->toArray();
        usort($temp, function ($a, $b) {
            return (float)$a['price'] <=> (float)$b['price'];
        });
        return $temp;
    }

    public static function dragStatusTip($item)
    {
        if ($item['purchase_status'] == 0 && strtotime($item['start_time']) < time() && strtotime($item['end_time']) > time()) {
            $status_val = 1;
            $status_tip = '正在接龙';
        } elseif (strtotime($item['end_time']) < time() || $item['purchase_status'] == 1) {
            $status_val = 2;
            $status_tip = '已结束';
        } else {
            $status_val = 3;
            $status_tip = '未开始';
        }
        return [$status_val, $status_tip];
    }

    public static function everyGoodsElement($goodsList)
    {
        $show_pics = [];
        if (!is_array($goodsList)) {
            $goodsList = $goodsList->toArray();
        }
        foreach ($goodsList as $item) {
            foreach ($item['pics'] as $value) {
                $show_pics[] = ['type' => $value['type'], 'src' => $value['src']];
                break;
            }
        }
        return $show_pics;
    }

    //接龙列表的核销状态显示，单条order接龙不用这个
    public static function verifyStatusTip($orders)
    {
        $flag = false;
        $val  = 3;
        $str  = '已取消';
        foreach ($orders as $item) {
            if ($flag) {
                break;
            }
            if ($item['status'] == 0) {
                if ($item['verify'] == 1) {
                    $flag = true;
                    $val  = 1;
                    $str  = '待核销';
                }
                if ($item['verify'] == 2) {
                    $flag = true;
                    $val  = 2;
                    $str  = '已核销';
                }
            }
        }
        return [$val, $str];
    }

    public static function formatNumber($number)
    {
        if (strpos((string)$number, '.') !== false) {
            return rtrim(rtrim($number, '0'), '.');
        }
        return (int)$number; // 转为整数
    }
}
